﻿using AIStudio.Wpf.Controls.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace MonitorPrism_Practice.Helper
{
    public static class WindowHelper
    {
        // 定义附加属性 IsCloseButtonEnabled
        public static readonly DependencyProperty IsCloseButtonEnabledProperty =
            DependencyProperty.RegisterAttached(
                "IsCloseButtonEnabled",
                typeof(bool),
                typeof(WindowHelper),
                new PropertyMetadata(false, OnIsCloseButtonEnabledChanged));

        // 获取附加属性 IsCloseButtonEnabled
        public static bool GetIsCloseButtonEnabled(DependencyObject obj)
        {
            return (bool)obj.GetValue(IsCloseButtonEnabledProperty);
        }

        // 设置附加属性 IsCloseButtonEnabled
        public static void SetIsCloseButtonEnabled(DependencyObject obj, bool value)
        {
            obj.SetValue(IsCloseButtonEnabledProperty, value);
        }

        // 当 IsCloseButtonEnabled 属性改变时触发的回调函数
        private static void OnIsCloseButtonEnabledChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is Button button && e.NewValue is bool isEnabled)
            {
                if (isEnabled)
                {
                    button.Click += Button_Click;
                }
            }
        }
        // 关闭命令执行处理器
        private static void Button_Click(object sender, RoutedEventArgs e)
        {
            var window = VisualHelper.TryFindParent<Window>(sender as DependencyObject);
            window.Close();
        }
    }
}
